﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BlackBird.Models;

namespace BlackBird.DataAccess
{
    public static class SupplierDataAccess
    {
        static List<SupplierDM> all_supplier = new List<SupplierDM>();

        public static List<SupplierDM> SupplierList
        {
            get { return AllSuppliersDM(); }
        }

        public static List<SupplierDM> AllSuppliersDM()
        {
            if (all_supplier.Count > 0) return all_supplier;

            using (var dbContext = new blackbirdEntities())
            {
                dbContext.supplier.ToList().ForEach(p => all_supplier.Add(BuildSupplierDM(p)));            
            }

            return all_supplier;
        }

        public static void AddSupplier(SupplierDM dm)
        {
            var findSupplier = all_supplier.Find(p => p.Name.Equals(dm.Name));
            if (findSupplier != null) return;

            all_supplier.Add(dm);
        }

        public static supplier SaveToDatabase(SupplierDM supplierDm)
        {
            using (var dbContext = new blackbirdEntities())
            {
                int supplierDmId = Convert.ToInt32(supplierDm.Id);
                //这里也判断同名的情况，避免同一个供应商被添加多次
                var supplierDb = dbContext.supplier.FirstOrDefault(p => 
                    p.idSupplier == supplierDmId);

                //update exist entity
                if (supplierDb != null)
                {
                    supplierDb.Name = supplierDm.Name;
                    supplierDb.Address = supplierDm.Address;

                    supplierDb.contactor.RealName = supplierDm.ContactorInfo.RealName;
                    supplierDb.contactor.Address = supplierDm.ContactorInfo.Address;
                    supplierDb.contactor.Zipcode = supplierDm.ContactorInfo.Zipcode;
                    supplierDb.contactor.Email = supplierDm.ContactorInfo.Email;
                    supplierDb.contactor.Tel = supplierDm.ContactorInfo.Tel;
                    supplierDb.contactor.Phone = supplierDm.ContactorInfo.Phone;

                    dbContext.SaveChanges();
                }
                else //add new entity
                {
                    contactor contactorDb = DBUtils.ConvertDMToDB(supplierDm.ContactorInfo);
                    dbContext.contactor.AddObject(contactorDb);

                    supplierDb = supplier.Createsupplier(0, contactorDb.idContactor);
                    supplierDb.Name = supplierDm.Name;
                    supplierDb.Address = supplierDm.Address;

                    dbContext.supplier.AddObject(supplierDb);
                    dbContext.SaveChanges();

                    //update the id for DM after db save changes
                    supplierDm.Id = supplierDb.idSupplier.ToString();
                }

                return supplierDb;
            }
        }

        public static SupplierDM BuildSupplierDM(supplier dbObject)
        {
            return new SupplierDM()
            {
                Id = dbObject.idSupplier.ToString(),
                Name = dbObject.Name,
                Address = dbObject.Address,
                ContactorInfo = new Contactor
                {
                    RealName = dbObject.contactor.RealName,
                    Tel = dbObject.contactor.Tel
                }
            };             
        }
    }
}